var isWidescreen=screen.width>=1280; 
if(isWidescreen){document.write("<style type='text/css'>.demo{width:1194px;}</style>");}
$(function(){
	setAjaxError(function(r,t,e){
		//alert("数据加载失败");
		$("#loadding").slideUp("slow", function(){
			mouseOver();
			isloading = false;
		});
	});
	
	function item_callback(){ 
		mouseOver();
		item_masonry();	
	}
	item_callback();
	
	var isloading = false;
	var page = 1;
	var isfinish = false;
	$(document).scroll(function(){
		if (isloading || isfinish) return;
		if (getScrollTop()+getClientHeight()==getScrollHeight()) {
			isloading = true;
			$("#loadding").slideDown('slow',function(){
				$.get(ctx + "/data/" + (++page), function(data) {
					if (data.length == 0) {
						isfinish = true;
						$("#loadding").html("对不起，已经没有数据了");
					} else {
						var arr = new Array();
						for (var i = 0; i < data.length; i++) {
							var item = data[i];
							fillTemplate(item);
							var newItem = $($("#item_template").html());
							$("#item_list").append(newItem);
							arr.push(newItem[0]);
						}
						$("#item_list").masonry("appended", $(arr), false);
						$("#loadding").slideUp("slow", function(){
							mouseOver();
							isloading = false;
						});
					}
				});
			});
		}
	});
});

function fillTemplate(item) {
	$("#img_link").attr("href", "#");
	$("#good_img").attr("alt", item.name).attr("src", ctx + "/" + item.bigImg);
	if (item.shopPrice == null) {
		$("#good_price").text("暂无报价");
	} else {
		$("#good_price").text("￥" + item.shopPrice);
	}
	$("#addCar_btn").attr("href", "");
	$("#good_name").text(item.name);
	$("#like_btn").attr("href", "");
	$("#like_count").text("100");
	$("#comment_link").attr("href", "");
	$("#comment_count").text("(10)");
}

function mouseOver() {
	$('.item').mouseover(function(){
		$(this).css('box-shadow', '0 1px 5px rgba(35,25,25,0.5)');
		$('.btns',this).show();
	}).mouseout(function(){
		$(this).css('box-shadow', '0 1px 3px rgba(34,25,25,0.2)');
		$('.btns',this).hide();		 	
	});
}

function item_masonry(){ 
	$('.item img').load(function(){ 
		$('.infinite_scroll').masonry({ 
			itemSelector: '.masonry_brick',
			columnWidth:226,
			gutterWidth:15								
		});		
	});
	$('.infinite_scroll').masonry({ 
		itemSelector: '.masonry_brick',
		columnWidth:226,
		gutterWidth:15								
	});
}


//JavaScript Document
/********************
* 取窗口滚动条高度 
******************/
function getScrollTop()
{
    var scrollTop=0;
    if(document.documentElement&&document.documentElement.scrollTop)
    {
        scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body)
    {
        scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}


/********************
* 取窗口可视范围的高度 
*******************/
function getClientHeight()
{
    var clientHeight=0;
    if(document.body.clientHeight&&document.documentElement.clientHeight)
    {
        var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;        
    }
    else
    {
        var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;    
    }
    return clientHeight;
}

/********************
* 取文档内容实际高度 
*******************/
function getScrollHeight()
{
    return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}

//------------------------判断窗体滚动条到达底部----------------------------

function reachBottom(){
        var scrollTop = 0;
        var clientHeight = 0;
        var scrollHeight = 0;
        if(document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
        } else if (document.body) {
        scrollTop = document.body.scrollTop;
        }
        if(document.body.clientHeight && document.documentElement.clientHeight) {
                clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
        } else {
                clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
        }
        scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
        if(scrollTop + clientHeight == scrollHeight) {
                return true;
        } else {
                return false;
        }
}
